﻿#region [YawControl Using Statements]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using WP7_XNA_Accelerometer_Simulator.GameObjects.SuperClasses;
using WP7_XNA_Accelerometer_Simulator.Utils.Accelerometer;
using WP7_XNA_Accelerometer_Simulator.Utils.GameInfo;
using WP7_XNA_Accelerometer_Simulator.GameObjects.GameModels;
using WP7_XNA_Accelerometer_Simulator.Utils.Intersection;
using WP7_XNA_Accelerometer_Simulator.Utils.Rotation;
#endregion

namespace WP7_XNA_Accelerometer_Simulator.GameObjects.MovementControls
{
    /// <summary>
    /// This Class represents the control responsible for applying yaw rotation
    /// to the Windows Phone 7
    /// </summary>
    public class YawControl : GameObject3D
    {
        #region [YawControl Fields]
        private float _xDif;
        private float _yCenter;
        private float _sensitivity;
        #endregion

        #region [YawControl Constructor]
        /// <summary>
        /// YawControl Contructor
        /// </summary>
        public YawControl()
        {
            Scale = new Vector3(0.011F, 0.011F, 0.011F);
            Position = new Vector3(4.5f, 4, 2);
            _xDif = 0;
            _sensitivity = 1f;
            _yCenter = 140F;
        }
        #endregion

        #region [YawControl LoadContent and Update Methods]
        /// <summary>
        /// See <see cref="GameObject3D.LoadContent"/> for a description
        /// </summary>
        public override void LoadContent(ContentManager content)
        {
            GameModel = content.Load<Model>(GameConstants.GreenKnotUri);
            base.LoadContent(content);
        }

        /// <summary>
        /// See <see cref="GameObject3D.Update"/> for a description
        /// </summary>
        /// <remarks>
        /// Check mouse intersection with this model and apply rotation accordingly
        /// </remarks>
        public override void Update(GameTime gameTime)
        {
            Matrix modelPosition = Matrix.CreateTranslation(Position);
            Ray cursorRay = GameManager.Cursor.CalculateCursorRay(GameManager.CameraProjectionMatrix, GameManager.CameraViewMatrix);
            
            if (GameManager.CurrentMouseState.LeftButton == ButtonState.Pressed &&
                GameManager.PreviousMouseState.LeftButton == ButtonState.Pressed)
            {
                _xDif = GameManager.PreviousMouseState.X - GameManager.CurrentMouseState.X;

                /*check intersection*/
                if (Intersection.RayIntersectsModel(cursorRay, GameModel, modelPosition, BonesCountMatrix, Scale.X))
                {
                    /*
                     * only apply rotation if the cursor's deslocation is superior to the chosen
                     * sensitivity. This is used to prevent unwanted deslocation in a direction
                     * that is not intended. This is actually not needed in this control since it 
                     * only deals with a rotation around one axis (yaw). However its here for coherence
                     * with the PitchRollControl.
                     */
                    if (Math.Abs(_xDif) > _sensitivity)
                    {
                        if (GameManager.CurrentMouseState.Y > _yCenter)
                        {
                            _xDif *= -1;
                        }

                        /*process rotation for each object on the accelerometer environment*/
                        foreach (GameObject3D gameObject3D in GUI.GameObjects3D)
                        {
                            gameObject3D.ProcessRotation(RotationUtil.RotationType.Yaw, _xDif);
                        }
                    }
                }
            }
        }
        #endregion
    }
}
